﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using WLong.BLL;

namespace WL_WinForm
{
    public partial class FrmCategory : DockContent
    {
        public FrmCategory()
        {
            InitializeComponent();
            Init();
            BindData();
            BindCategory();
        }

        private void BindData()
        {
            OtherCategory other = new OtherCategory();
            DataSet ds = other.GetList("Disabled=0");
            gvOtherCategory.DataSource = ds.Tables[0];
        }

        private void BindCategory()
        {
            Category category = new Category();
            DataSet ds = category.GetList("Disabled=0");
            gvCategory.DataSource = ds.Tables[0];
        }

        private void Init()
        {
            cbGrade.DisplayMember = "LName";
            cbGrade.ValueMember = "LValue";
            cbGrade.DataSource = BLL.wlBLL.BllComboList.GetGrade();
            cbGrade.SelectedIndex = 0;

            DataTable dt1 = BLL.wlBLL.BllComboList.GetHighId();
            dt1 = COMMON.ControlCommon.ConvertToComboList(dt1, "CategoryName", "CategoryID");
            cbHighId.DataSource = null;
            COMMON.ControlCommon.ComboBinds(dt1, cbHighId);
        }

        #region 信息类型

        private void btnAdd_Click(object sender, EventArgs e)
        {
            OtherCategory other = new OtherCategory();
            if (txtOtherCategoryID.Text != "")
            {
                WLong.Model.OtherCategory mOther = new WLong.Model.OtherCategory();
                mOther.OtherCategoryID = new Guid(txtOtherCategoryID.Text);
                mOther.OtherCategoryCode = txtOtherCategoryCode.Text.Trim();
                mOther.OtherCategoryName = txtOtherCategoryName.Text.Trim();
                mOther.OtherCategoryGroup = txtOtherCategoryGroup.Text.Trim();
                if (txtSort.Text != "")
                {
                    mOther.Sort = int.Parse(txtSort.Text.Trim());
                }
                mOther.Disabled = 0;
                mOther.REC_ModifyBy = COMMON.Login.loginName;
                mOther.REC_ModifyTime = DateTime.Now;

                if (other.Update(mOther))
                {
                    ClearControl();
                    BindData();
                    COMMON.ControlCommon.ShowMessageBoxOk("修改成功！");
                }
                else
                {
                    COMMON.ControlCommon.ShowMessageBoxOk("修改失败！");
                }
            }
            else
            {
                WLong.Model.OtherCategory mOther = new WLong.Model.OtherCategory();
                mOther.OtherCategoryID = Guid.NewGuid();
                mOther.OtherCategoryCode = txtOtherCategoryCode.Text.Trim();
                mOther.OtherCategoryName = txtOtherCategoryName.Text.Trim();
                mOther.OtherCategoryGroup = txtOtherCategoryGroup.Text.Trim();
                if (txtSort.Text != "")
                {
                    mOther.Sort = int.Parse(txtSort.Text.Trim());
                }
                mOther.Disabled = 0;
                mOther.REC_CreateBy = COMMON.Login.loginName;
                mOther.REC_CreateTime = DateTime.Now;

                other.Add(mOther);
                ClearControl();
                BindData();
            }
        }

        private void ClearControl()
        {
            txtOtherCategoryID.Text = "";
            txtOtherCategoryCode.Text = "";
            txtOtherCategoryName.Text = "";
            txtOtherCategoryGroup.Text = "";
            txtSort.Text = "";
        }

        private void gvOtherCategory_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            txtOtherCategoryID.Text = gvOtherCategory.CurrentRow.Cells["OtherCategoryID"].Value.ToString();
            txtOtherCategoryCode.Text = gvOtherCategory.CurrentRow.Cells["OtherCategoryCode"].Value.ToString();
            txtOtherCategoryName.Text = gvOtherCategory.CurrentRow.Cells["OtherCategoryName"].Value.ToString();
            txtOtherCategoryGroup.Text = gvOtherCategory.CurrentRow.Cells["OtherCategoryGroup"].Value.ToString();
            txtSort.Text = gvOtherCategory.CurrentRow.Cells["Sort"].Value.ToString();
        }

        private void gvOtherCategory_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                OtherCategory other = new OtherCategory();
                WLong.Model.OtherCategory mOther = new WLong.Model.OtherCategory();
                mOther.OtherCategoryID = new Guid(gvOtherCategory.CurrentRow.Cells["OtherCategoryID"].Value.ToString());
                mOther.OtherCategoryCode = gvOtherCategory.CurrentRow.Cells["OtherCategoryCode"].Value.ToString();
                mOther.OtherCategoryName = gvOtherCategory.CurrentRow.Cells["OtherCategoryName"].Value.ToString();
                mOther.OtherCategoryGroup = gvOtherCategory.CurrentRow.Cells["OtherCategoryGroup"].Value.ToString();
                mOther.Sort = int.Parse(gvOtherCategory.CurrentRow.Cells["Sort"].Value.ToString());
                mOther.Disabled = 1;
                mOther.REC_ModifyBy = COMMON.Login.loginName;
                mOther.REC_ModifyTime = DateTime.Now;

                if (other.Update(mOther))
                {
                    BindData();
                    COMMON.ControlCommon.ShowMessageBoxOk("删除成功！");
                }
                else
                {
                    COMMON.ControlCommon.ShowMessageBoxOk("删除失败！");
                }
            }
        }


        #endregion

        #region 网站类型

        private void cbGrade_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbPriorId.DataSource = null;
            DataTable dt = BLL.wlBLL.BllComboList.GetPriorId(int.Parse(cbGrade.SelectedValue.ToString()));
            dt = COMMON.ControlCommon.ConvertToComboList(dt, "CategoryName", "CategoryID");
            COMMON.ControlCommon.ComboBinds(dt, cbPriorId);
        }

        private void btnAdd1_Click(object sender, EventArgs e)
        {
            Category category = new Category();
            if (txtCategoryID .Text !="")
            {
                WLong.Model.Category mCategory = new WLong.Model.Category();
                mCategory.CategoryID = new Guid(txtCategoryID.Text);
                mCategory.CategoryName = txtCategoryName.Text;
                mCategory.CategoryCode = txtCategoryCode.Text;
                mCategory.Grade =  int.Parse(cbGrade.SelectedValue.ToString());
                mCategory.PriorId = cbPriorId.SelectedValue.ToString() == "-999" ? new Guid(txtCategoryID.Text) 
                    : new Guid(cbPriorId.SelectedValue.ToString());
                mCategory.IsDisplay = ckIsDisplay.Checked ? 0 : 1;
                mCategory.HighId = cbHighId.SelectedValue.ToString() == "-999" ? new Guid(txtCategoryID.Text)
                    : new Guid(cbHighId.SelectedValue.ToString());
                if (txtSort1.Text !="")
                {
                    mCategory.Sort = int.Parse(txtSort1.Text);
                }
                mCategory.Disabled = 0;
                mCategory.REC_ModifyBy = COMMON.Login.loginName;
                mCategory.REC_ModifyTime = DateTime.Now;

                if (category.Update(mCategory))
                {
                    ClearCategory();
                    BindCategory();
                    COMMON.ControlCommon.ShowMessageBoxOk("修改成功！");
                }
                else
                {
                    COMMON.ControlCommon.ShowMessageBoxOk("修改失败！");
                }
            }
            else
            {
                WLong.Model.Category mCategory = new WLong.Model.Category();
                mCategory.CategoryID = Guid.NewGuid();
                mCategory.CategoryName = txtCategoryName.Text;
                mCategory.CategoryCode = txtCategoryCode.Text;
                mCategory.Grade = int.Parse(cbGrade.SelectedValue.ToString());
                mCategory.PriorId = cbPriorId.SelectedValue.ToString() == "-999" ? mCategory.CategoryID
                    : new Guid(cbPriorId.SelectedValue.ToString());
                mCategory.IsDisplay = ckIsDisplay.Checked ? 0 : 1;
                mCategory.HighId = cbHighId.SelectedValue.ToString() == "-999" ? mCategory.CategoryID
                    : new Guid(cbHighId.SelectedValue.ToString());
                if (txtSort1.Text != "")
                {
                    mCategory.Sort = int.Parse(txtSort1.Text);
                }
                mCategory.Disabled = 0;
                mCategory.REC_CreateBy = COMMON.Login.loginName;
                mCategory.REC_CreateTime = DateTime.Now;

                category.Add(mCategory);
                ClearCategory();
                BindCategory();
                Init();
            }
        }

        private void gvCategory_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            txtCategoryID.Text = gvCategory.CurrentRow.Cells["CategoryID"].Value.ToString();
            txtCategoryName.Text = gvCategory.CurrentRow.Cells["CategoryName"].Value.ToString();
            txtCategoryCode.Text = gvCategory.CurrentRow.Cells["CategoryCode"].Value.ToString();
            cbGrade.Text = gvCategory.CurrentRow.Cells["Grade"].Value.ToString();
            cbPriorId.SelectedValue = gvCategory.CurrentRow.Cells["PriorId"].Value.ToString();
            cbHighId.SelectedValue = gvCategory.CurrentRow.Cells["HighId"].Value.ToString();
            txtSort1.Text = gvCategory.CurrentRow.Cells["Sort"].Value.ToString();
            if (gvCategory.CurrentRow.Cells["IsDisplay"].Value.ToString() == "0")
            {
                ckIsDisplay.Checked = true;
            }
        }

        private void gvCategory_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                Category category = new Category();
                WLong.Model.Category mCategory = new WLong.Model.Category();
                mCategory.CategoryID = new Guid(gvCategory.CurrentRow.Cells["CategoryID"].Value.ToString());
                mCategory.CategoryName = gvCategory.CurrentRow.Cells["CategoryName"].Value.ToString();
                mCategory.CategoryCode = gvCategory.CurrentRow.Cells["CategoryCode"].Value.ToString();
                mCategory.Grade = int.Parse(gvCategory.CurrentRow.Cells["Grade"].Value.ToString());
                mCategory.PriorId = new Guid(gvCategory.CurrentRow.Cells["PriorId"].Value.ToString());
                mCategory.IsDisplay = int.Parse(gvCategory.CurrentRow.Cells["IsDisplay"].Value.ToString());
                mCategory.HighId = new Guid(gvCategory.CurrentRow.Cells["HighId"].Value.ToString());
                mCategory.Sort = int.Parse(gvCategory.CurrentRow.Cells["Sort"].Value.ToString());
                mCategory.Disabled = 1;
                mCategory.REC_ModifyBy = COMMON.Login.loginName;
                mCategory.REC_ModifyTime = DateTime.Now;

                if (category.Update(mCategory))
                {
                    COMMON.ControlCommon.ShowMessageBoxOk("删除成功！");
                }
                else
                {
                    COMMON.ControlCommon.ShowMessageBoxOk("删除失败！");
                }
            }
        }

        private void ClearCategory()
        {
            txtCategoryID.Text = "";
            txtCategoryName.Text = "";
            txtCategoryCode.Text = "";
            cbPriorId.SelectedValue = "-999";
            cbHighId.SelectedValue = "-999";
            txtSort1.Text = "";
            ckIsDisplay.Checked = true;
        }

        #endregion

    }
}
